package com.proposition.web.system;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.proposition.commons.constant.ErrorCode;
import com.proposition.commons.model.*;
import com.proposition.exam.service.ExamAcupointAdultService;
import com.proposition.support.utils.Rest;
import com.sun.xml.internal.xsom.impl.scd.Iterators;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 系统参数设置
 *
 * @author 杨凯
 * @create 2017-11-03 9:02
 **/
@Slf4j
@Controller
@RequestMapping("/system")
public class SystemParamController {


	@Autowired
	private ExamAcupointAdultService adultService;


	@RequiresRoles("admin")
	@RequestMapping("/systemParam")
	public String systemParamPage(Model model) {
		/*试题参数*/
		model.addAttribute("examParamWendaAcupoint", new ExamParamWendaAcupoint().selectOne(new EntityWrapper()));
		model.addAttribute("examParamWendaFSENC", new ExamParamWendaFSENC().selectOne(new EntityWrapper()));
		model.addAttribute("examParamAcupointLocationAdult", new ExamParamAcupointLocationAdult().selectOne(new EntityWrapper()));
		model.addAttribute("examParamAcupointLocationPediatric", new ExamParamAcupointLocationPediatric().selectOne(new EntityWrapper()));
		model.addAttribute("examParamSinglePloyCEC", new ExamParamSinglePloyCEC().selectOne(new EntityWrapper()));
		model.addAttribute("examParamCompletePloyFSEN", new ExamParamCompletePloyFSEN().selectOne(new EntityWrapper()));
		return "system/system-param";
	}


	/**
	 * 获取客观化成人穴位
	 *
	 * @return
	 */
	@ResponseBody
	@GetMapping("/getAdultAcupoint")
	public Map<String, Object> getAdultAcupoint() {
		Map<String, Object> map = new HashMap<>();
		Wrapper<ExamAcupointAdult> wrapper = new EntityWrapper<>();
		wrapper.setSqlSelect("id", "stem as text");
		wrapper.eq("check_status_id", 4).eq("category_id", "16");
		List<Map<String, Object>> list = adultService.selectMaps(wrapper);
		map.put("results", list);
		return map;
	}


	@ResponseBody
	@GetMapping("/getSetAcupoint")
	public Map<String, Object> getSetAcupoint() {
		Map<String, Object> map = new HashMap<>();
		List<SetAcupoint> setAcupoints = new SetAcupoint().selectAll();
		map.put("results", setAcupoints);
		return map;
	}


	/**
	 * 设置穴位出现次数
	 *
	 * @return
	 */
	@PostMapping("/setAcupoint")
	@ResponseBody
	public Rest setAcupoint(@RequestBody List<SetAcupoint> list) {
		Rest.Builder rest = Rest.newBuilder();
		try {
			//设置新的穴位前删除原有穴位记录
			new SetAcupoint().delete(new EntityWrapper());
			for (SetAcupoint setAcupoint : list) {
				setAcupoint.insert();
			}
			return rest.status(ErrorCode.SUCCESS).build();
		} catch (Exception e) {
			log.error("系统设置===>设置穴位出现异常[{}]",e.getMessage());
			return rest.status(ErrorCode.UNEXPECTED_ERROR).build();
		}
	}

}
